import requests
from lxml import etree
from Chaojiying_Python.chaojiying import Chaojiying_Client

url = 'https://so.gushiwen.org/user/login.aspx?from=http://so.gushiwen.org/user/collect.aspx'
header = {
            'User-Agent': 'Mozilla/5.0(Windows NT 10.0;WOW64)AppleWebKit/537.36(KHTML, like Gecko) Chrome / 86.0.4240.198 Safari / 537.36'
}
res = requests.get(url, headers=header)
res.encoding = 'utf-8'
res = res.text

tree = etree.HTML(res)
wenjian = tree.xpath('//div[@class="mainreg2"]//img[@id="imgCode"]/@src')
url2 = 'https://so.gushiwen.org/' + wenjian[0]
res2 = requests.get(url2, headers=header)
res2.encoding = 'utf-8'
res2 = res2.content
fp = open('../yanzheng.jpg', 'wb')
fp.write(res2)

# 获取验证码
chaojiying = Chaojiying_Client()
yanzhengma = chaojiying.PostPic(res2, 1902)
print('验证码：' + yanzhengma['pic_str'])

url3 = 'https://so.gushiwen.org/user/login.aspx?from=http%3a%2f%2fso.gushiwen.org%2fuser%2fcollect.aspx'

data = {
    '__VIEWSTATE': 'L3/JdzKIJfFHKdAgbGcg5Qj2lvAe+2fAY+z9iqNdR/0k4DPMLiQB4mkg7b579fpXqXB85T4zxsxRuHvyWtfxUX+9Ttb1ZzpGawvrpLEhfnwuXiILSJo4I5rWPhs=',
    '__VIEWSTATEGENERATOR': 'C93BE1AE',
    'from': 'http://so.gushiwen.org/user/collect.aspx',
    'email': '2289426873@qq.com',
    'pwd': 'guoxin0814..',
    'code': yanzhengma['pic_str'],
    'denglu': '登录'
}

neirong = requests.post(url=url3, data=data, headers=header)
neirong.encoding = 'utf-8'
neirong = neirong.text
print(neirong)
fp2 = open('../neirong.html', 'w', encoding='utf-8')
fp2.write(neirong)
print('保存成功')



